﻿
using System.Windows;
using System.Windows.Media;
using Red.Controls.Utils;
using Red.Core;

namespace Red.Controls.Depot.ViewModels
{
    /// <summary>
    ///  
    /// </summary>
    public class FileViewItemResource : FileViewItem
    {
        #region Data

        private string _revisionInfo;
        protected readonly DepotInfo _depot;

        #endregion

        #region Properties

        public string RevisionInfo
        {
            get { return _revisionInfo; }
            set
            {
                if ( _revisionInfo == value )
                    return;

                _revisionInfo = value;
                OnPropertyChanged();
            }
        }

        #endregion Properties

        #region C-tor

        public FileViewItemResource( string path, string title, ImageSource thumbnail, string group, bool isEnabled, DepotInfo depot )
            : base( path, title, thumbnail, group, isEnabled )
        {
            RevisionInfo = "not in depot";
            _depot = depot;
        }

        #endregion C-tor

        #region Public methods

        public override bool IsVersionControlItem()
        {
            return true;
        }

        public override IDataObject GetDraggedData()
        {
            var depotPath = _depot.GetDepotPath( Path );
            if ( depotPath != null )
            {
                return new DataObject( "RTTIValueHolder", depotPath );
            }
            else
            { // not-in-depot fallback
                return new DataObject( "FullFilePath", Path );
            }
        }

        #endregion Public methods

        #region Protected methods

        protected override void OnP4MetaDataChanged()
        {
            var headRevision = _p4MetaData.HeadRev;
            var haveRevision = _p4MetaData.HaveRev;
            var revisionText = "";
            P4FileState = P4FileState.P4FS_InDepot;
            if ( headRevision > haveRevision )
            {
                IsOutOfDate = true;
                revisionText = "not latest revision of file";
            }
            else if ( headRevision == haveRevision )
            {
                revisionText = "latest revision of file";
                IsOutOfDate = false;
            }

            if ( _depot.SourceControl.IsCheckedOutByMe( _p4MetaData ) )
            {
                P4FileState = P4FileState.P4FS_CheckedOut;
                revisionText += "\n" + GetChangelistRevisionText( "is checked out in", _p4MetaData.Change );
                RevisionInfo = string.Format( "{0} (#{1} of {2})", revisionText, haveRevision, headRevision );
            }
            else if ( _depot.SourceControl.IsMarkedForAdd( _p4MetaData ) )
            {
                P4FileState = P4FileState.P4FS_MarkedForAdd;
                RevisionInfo = GetChangelistRevisionText( "is marked for add in", _p4MetaData.Change );
            }
            else if ( _depot.SourceControl.IsDeleted( _p4MetaData ) )
            {
                P4FileState = P4FileState.P4FS_Deleted;
                RevisionInfo = GetChangelistRevisionText( "is deleted in", _p4MetaData.Change );
            }
            else
            {
                RevisionInfo = string.Format( "{0} (#{1} of {2})", revisionText, haveRevision, headRevision );
            }
        }

        #endregion Protected methods

        #region Private methods

        private string GetChangelistRevisionText( string text, int changelistId )
        {
            if ( changelistId == 0 )
                return text + " default changelist";

            return string.Format( "{0} changelist {1}", text, changelistId );
        }

        #endregion Private methods
    }
}
